const path = require('path')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const globalLessVars = require(path.join(__dirname, './src/utils/globalLessVars.js'))
const colorsLess = globalLessVars(path.join(__dirname, './src/style/theme.less'))
const inDev = process.env.NODE_ENV === 'development'

module.exports = {
  publicPath: process.env.BASE_URL || '/',
  lintOnSave: true,
  outputDir: inDev ? 'spa' : path.join(__dirname, 'spa'),
  productionSourceMap: false,

  // compiler: false,
  css: {
    // eslint-disable-next-line no-eval
    sourceMap: !!eval(process.env.GENERATE_CSS_MAP),
    loaderOptions: {
      postcss: {
        plugins: [
          require('postcss-plugin-px2rem')({
            rootValue: 75,
            exclude: /(node_module)/
          })
        ]
      },
      less: {
        modifyVars: colorsLess
      }
    }
  },

  chainWebpack: config => {
    config.output.chunkFilename(`js/[name]-[chunkhash].js`)

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons/svg'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  },

  devServer: {
    open: true,
    disableHostCheck: true,
    proxy: {
      '/api': {
        target: process.env.VUE_APP_API_HOST, //代理到这来
        changeOrigin: true //是否改变域名
      },
      '/storage': {
        target: process.env.VUE_APP_API_HOST,
        changeOrigin: true
      }
    }
  },

  pwa: {
    name: process.env.VUE_APP_NAME || 'blue',
    themeColor: '#59B6D7',
    msTileColor: '#59B6D7'
  }
}
